package com.chain.BrotherExchange.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.CoinOrderList;
import com.chain.BrotherExchange.ui.adapter.CoinOrderListAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.FirstItemMarginDecoration;
import com.chain.BrotherExchange.viewmodel.OrderViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.Bind;

/**
 * 币币订单列表
 */
public class CoinOrderListFragment extends AbsNetworkStateFragment
        implements OnRefreshLoadmoreListener, AdapterView.OnItemClickListener {

    @Bind(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @Bind(R.id.smart_refresh_layout)
    SmartRefreshLayout mSmartRefreshLayout;

    private OrderViewModel mViewModel;
    private int currentPage = 1;
    private CoinOrderListAdapter mCoinOrderListAdapter;

    @Override
    protected int getLayoutID() {
        return R.layout.simple_load_refresh_recycler_view;
    }

    @Override
    protected void initActions() {
        // 刷新加载监听
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(this);

        // 订单列表
        mCoinOrderListAdapter = new CoinOrderListAdapter();
        mRecyclerView.setAdapter(mCoinOrderListAdapter);
        mRecyclerView.addItemDecoration(new FirstItemMarginDecoration());
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mCoinOrderListAdapter.setCancelClickListener(this);

        // 请求订单列表
        mViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
        initCoinOrder();
    }

    private void initCoinOrder() {
        currentPage = 1;
        mViewModel.newCoinOrderList().observe(this, resource ->
                Resource.load(getContext(), resource, this, result -> {
                    mCoinOrderListAdapter.setOrder(result);
                    mSmartRefreshLayout.finishRefresh();
                })
        );
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 取消订单
        CoinOrderList.DataBean.ListBean bean = mCoinOrderListAdapter.getItem(position);
        mViewModel.cancelCoinOrder(bean.getEntrust_id()).observe(this, resource -> {
            switch (resource.state) {
                case ERROR:
                    Androids.makeText(resource.message);
                    break;
                case SUCCESS:
                    bean.setStates(3); // 改变订单状态
                    mCoinOrderListAdapter.notifyItemChanged(position);
                    break;
            }
        });
    }

    @Override
    public void onAnewRequest() {
        initCoinOrder();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        String token = Androids.LocalConfig.getToken();
        int uid = Androids.LocalConfig.getUid();

        DataRepository.get().coinOrderList(uid, token, ++currentPage, result -> {
            CoinOrderList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                List<CoinOrderList.DataBean.ListBean> list = resultData.getList();
                if (list != null && !list.isEmpty()) {
                    mCoinOrderListAdapter.addOrder(list);
                } else {
                    Androids.makeText(R.string.no_more);
                }

            } else if (result.isNotNetwork()) {
                Androids.makeText(R.string.network_error);
            } else {
                Androids.makeText(result.getMsg());
            }

            mSmartRefreshLayout.finishLoadmore(0);
        });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        initCoinOrder();
    }
}
